﻿using BCCommon;
using BCDto.Common.ErrorMessage;
using BCDto.TMS.Driver.DriverExamine;
using BCDto.TMS.Driver.DriverExamLearn;
using BCDto.TMS.Driver.DriverLearnDatum;
using BCService.TMS.Driver.DriverExamLearn;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System;

namespace BCTMSRESTful.Controllers.DriverApp.Driver
{
    /// <summary>
    /// 学习资料信息
    /// </summary>
    [ApiExplorerSettings(GroupName = "DriverApp")]
    [Route("api/v1/DriverApp/[controller]")]
    public class LearnDatumController : DriverAppBaseController
    {
        private readonly IDriverExamLearnService driverExamLearnService;

        public LearnDatumController(IDriverExamLearnService driverExamLearnService)
        {
            this.driverExamLearnService = driverExamLearnService;
        }

        /// <summary>
        /// 带有分页信息的查询学习资料
        /// </summary>
        /// <param name="datumTopic">学习标题</param>
        /// <param name="answerContent">学习答案</param>
        /// <param name="index">分页页码</param>
        /// <param name="size">页数</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<DriverLearnDatumDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult GetDriverLearnDatums(
            [FromQuery(Name = "datum_topic")] string datumTopic,
            [FromQuery(Name = "answer_content")] string answerContent,
            [FromQuery(Name = "index")] int index = 1,
            [FromQuery(Name = "size")] int size = 10)
        {
            if (!this.Driver.Role.HasValue)
            {
                throw new ArgumentException("请先完善车辆信息，再参加培训考试。");
            }
            return Ok(driverExamLearnService.GetDriverLearnDatums(datumTopic, answerContent, index, size, this.Driver.Role.Value));
        }


        /// <summary>
        /// 获取当前司机的考试成绩
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(DriverExamineMarkDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult ExamineMark()
        {
            return Ok(driverExamLearnService.GetExamineMarkByDriverId(this.Driver.DriverId));
        }

        /// <summary>
        /// 获取考试题列表
        /// </summary>      
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<DriverExamineTestQuestionDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult ExamineTestquestions()
        {
            if (!this.Driver.Role.HasValue)
            {
                throw new ArgumentException("请先完善车辆信息，再参加培训考试。");
            }
            return Ok(driverExamLearnService.GetExamineTestquestions(this.Driver.Role.Value));
        }

        /// <summary>
        /// 获取考试题对应的答案列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]/{paper_id}")]
        [ProducesResponseType(typeof(PageableList<ExamineQuestionAnswerDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult QuestionAnswers([FromRoute(Name = "paper_id")] long paperId)
        {
            return Ok(driverExamLearnService.GetExamineQuestionAnswers(paperId));
        }

        /// <summary>
        /// 生成当前司机的考试成绩
        /// </summary>
        /// <param name="dto">考试成绩信息</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [Log("生成当前司机的考试成绩")]
        [ProducesResponseType(typeof(DriverExamineMarkDto), 201)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult CreateExamineMark([FromBody] DriverExamineMarkRequestDto dto)
        {
            var result = driverExamLearnService.CreateExamineMark(this.Driver.DriverId, dto);

            return Created("/api/v1/DriverApp/LearnDaturm/ExamineMark", result);
        }
    }
}